// pages/detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    messinfo: null,
    replyList: [],
    InputBottom: 0,
    replyinfo:'',
    islike:false,
  },
  InputFocus(e) {
    this.setData({
      InputBottom: e.detail.height
    })
  },
  InputBlur(e) {
    this.setData({
      InputBottom: 0
    })
  },
  InputValue(e){
    this.setData({
      replyinfo: e.detail.value,
    })
  },  

  formDate(data){
      const year = data.date.getFullYear();
      const month = data.date.getMonth() + 1;
      const day = data.date.getDate();
      const date = year+"年"+month+"月"+day+"日";
      data.fdate = date;
      return data;
  },

  submit(){
    const db = wx.cloud.database();
    let userInfo = wx.getStorageSync('userInfo')
    let _date = new Date()
    let that  = this
    let _data = {
      date: _date,
      reply: that.data.replyinfo,
      replyId: that.data.messinfo._id,
      replyName: userInfo.nickName,
      avatarUrl: userInfo.avatarUrl,
    }
    db.collection('replyInfo').add({
      data: _data
    })
    .then(res =>{
      this.setData({
        replyinfo: ''
      })
      let _replyList = this.data.replyList
      _replyList.push(_data)
      this.setData({
        replyList: _replyList
      })
      //console.log(res)
    })
    .catch(err =>{
      //console.log(err)
    })
    let rNum = this.data.messinfo.replyNum + 1
    db.collection('messageInfo').doc(that.data.messinfo._id).update({
      data: {
        replyNum : rNum
      }
    })
    .then(res =>{

      
     // console.log(res)
    })
    .catch(err =>{
      //console.log(err)
    })
  },

  formDateList(data){
    for(let e of data){
      const year = e.date.getFullYear();
      const month = e.date.getMonth() + 1;
      const day = e.date.getDate();
      const date = year+"年"+month+"月"+day+"日";
      e.fdate = date;
    }
    return data;
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const db = wx.cloud.database();
    //获取帖子信息
    db.collection('messageInfo').where({
      _id: options.messid
    })
    .get().then(res => {
      res.data = this.formDate(res.data[0])
      this.setData({
        messinfo: res.data,
      })
      console.log(this.data.messinfo)
      //增加浏览量
      let sNum = res.data.seeNum + 1
      db.collection('messageInfo').doc(res.data._id).update({
      data: {
        seeNum : sNum
      }
    })
    })

    //获取回复信息
    db.collection('replyInfo').where({
      replyId: options.messid
    })
    .get().then(res => {
      res.data = this.formDateList(res.data)
      this.setData({
        replyList: res.data,
      })
    })

    
  },
  
  switchlike(){
    const db = wx.cloud.database();
    if(this.data.islike){
      this.setData({
        islike: false
      })
      let lNum = this.data.messinfo.likeNum - 1
      this.setData({
        'messinfo.likeNum' : lNum
      })
      db.collection('messageInfo').doc(this.data.messinfo._id).update({
        data: {
          likeNum: lNum
        }
      })
    }
    else{
      this.setData({
        islike: true
      })
      let lNum = this.data.messinfo.likeNum + 1
      this.setData({
        'messinfo.likeNum' : lNum
      })
      db.collection('messageInfo').doc(this.data.messinfo._id).update({
        data: {
          likeNum: lNum
        }
      })
    }
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})